<?php
/**
 * Created by PhpStorm.
 * User: dwlw
 * Date: 2018/5/7
 * Time: 10:25
 */
namespace app\admin\controller;
use think\Controller;
use app\admin\model\Caser;
use think\Db;

class About extends Base
{

    public function edit()
    {
        if ($this->request->isPost()){
            $this->editConfig();
        }


        $config = $this->getConfig();
        $this->assign('config', $config);
        return $this->fetch();
    }

    public function hredit ()
    {
        if ($this->request->isPost()){
            $this->get_edit();
        }else{
            $id = input('id');
            if (isset($id) && is_numeric($id)) {
                $case = Db::table('d_job')->find($id);
            }

            if (empty($case)) {
                return $this->error('没有找到该数据');
            }

            $this->assign('case',$case);
            return $this->fetch();
        }
    }


    public function hrlst ()
    {
        $case = new Caser();
        $casies = $case->table('d_job');

        $title = input('title');
        if (isset($title)) {
            $casies->where('title', 'like', "%{$title}%");
        }

        $resu = $casies->order('id','desc')->paginate(10);
        $page = $resu->render();
        $this->assign('casies', $resu);
        $this->assign('page', $page);
        return $this->fetch();
    }

    public function hradd ()
    {
        if ($this->request->isPost()){
            $this->get_add();
        }else{
            return $this->fetch();
        }
    }

    public function get_add(){
        $datas = input('post.');
        $datas['created_at'] = date('Y-m-d H:i:s');
        //重组数组数据
        $cl = new Caser();

        $resu = $cl->table('d_job')->insert($datas);

        if(!$resu){
            $this->error('添加错误');
        }else{
            $this->success('新增成功', 'about/hrlst');
        }
    }

    public function destroy() {
        $id = input('id');
        if (isset($id) && is_numeric($id)) {
            Db::table('d_job')->delete($id);
        }
        $this->success('删除成功', 'about/hrlst');
    }

    //处理add提交数据
    public function get_edit(){
        $datas = input('post.');
        $id = $datas['id'];
        unset($datas['id']);

        $flag = true;

        if (isset($id) && is_numeric($id)) {
            Db::table('d_job')->where('id', $id)->update($datas);
            $flag = false;
        }

        if($flag){
            $this->error('编辑失败', 'Users/edit' , ['id'=>$id]);
        }else{
            $this->redirect('/admin/about/hredit?id='.$id);
        }
    }


    public function teamadd ()
    {
        if ($this->request->isPost()){
            $this->get_team_add();
        }else{
            return $this->fetch();
        }
    }

    public function teamedit ()
    {
        if ($this->request->isPost()){
            $this->get_team_edit();
        }else {
            $id = input('id');
            if (isset($id) && is_numeric($id)) {
                $case = Db::table('d_team')->find($id);
            }

            if (empty($case)) {
                return $this->error('没有找到该数据');
            }

            $this->assign('case', $case);
            return $this->fetch();
        }
    }

    public function teamlst ()
    {
        //获取用户信息
        $case = new Caser();
        $casies = $case->table('d_team');

        $title = input('title');
        if (isset($title)) {
            $casies->where('name', 'like', "%{$title}%");
        }

        $resu = $casies->order('id','desc')->paginate(10);
        $page = $resu->render();
        $this->assign('casies', $resu);
        $this->assign('page', $page);
        return $this->fetch();
    }

    //处理add提交数据
    public function get_team_add(){
        $datas = input('post.');
        $datas['updated_at'] = date('Y-m-d H:i:s');
        //重组数组数据
        $cl = new Caser();
        $resu = $cl->table('d_team')->insert($datas);

        if(!$resu){
            $this->error('添加错误');
        }else{
            $this->success('新增成功', 'about/teamlst');
        }
    }

    public function teamdestroy() {
        $id = input('id');
        if (isset($id) && is_numeric($id)) {
            Db::table('d_team')->delete($id);
        }
        $this->success('删除成功', 'about/teamlst');
    }


    //处理add提交数据
    public function get_team_edit(){
        $datas = input('post.');
        $id = $datas['id'];
        unset($datas['id']);

        $flag = true;
        if (isset($id) && is_numeric($id)) {
            Db::table('d_team')->where('id', $id)->update($datas);
            $flag = false;
        }

        if($flag){
            $this->error('编辑失败', 'about/teamedit' , ['id'=>$id]);
        }else{
            $this->redirect('/admin/about/teamedit?id='.$id);
        }
    }
}
